<?php


namespace App\Service\PayApi;


use App\Service\RateCustomizedService;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Utils\ApplicationContext;
use PHPUnit\Framework\MockObject\CannotUseAddMethodsException;

/**
 * Class PayCurrencyAndAmountService
 *
 * @package App\Service\PayApi
 */
class PayCurrencyAndAmountService {

    /**
     * @var RateCustomizedService
     * @Inject()
     */
    private $RateCustomizedService;

    /**
     * @param $mchConfig
     * @param $channelList
     * @param $orderDetail
     *
     * @return mixed
     * @throws \Exception
     */
    public function calcCurrencyAndAmount ($mchConfig, $channelList, $orderDetail) {

        $mchTransCurrency = $orderDetail['mchTransCurrency'];
        $mchTransAmount   = $orderDetail['mchTransAmount'];

        if (!$channelList) {
            throw new \Exception(trans('pay.channel.no_available'));
        }

        $bankResult = null;
        try {

            // 获取汇率计算结果
            $req = [

                // 汇率组
                'rateGroupId'       => $mchConfig['settleExchangeRateId'],  // 汇率组id

                'mchTransCurrency'  => $mchTransCurrency, // 商户上送平台交易币种
                'mchTransAmount'    => $mchTransAmount, // 商户上送平台交易金额（分）
                'mchSettleCurrency' => $mchConfig['settleCurrency'], //商户和平台的结算币种

                'channelList' => $channelList,
            ];

            $bankResult = $this->RateCustomizedService->getSettleRateAndAmount($req);

            $bankResult = $bankResult['data'];

        } catch (\Throwable $e) {

            CustomUtils::xzxDebug($e->getMessage());

            throw new \Exception(trans('pay.rate.fail'));
        }

        return $bankResult;
    }
}
